(function($) {
	var methods = {
			"init": function(options){
				$.extend( $.vlpi.global, options );
				return this.each(function(){
					$.vlpi.create();
				});
			}
		};

	$.vlpi = {};
	
	$.vlpi.global = {
		"ldap": null,
		"containingDiv": null,
		"userMods": null,
		"messages":null,
		"intervals":{}
	};
	
	$.fn.viewLoader = function( method ) {	    
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			alert($.error( 'Method ' +  method + ' does not exist on jQuery.dialogPrompt' ));
			$.error( 'Method ' +  method + ' does not exist on jQuery.dialogPrompt' );
		}
	};
	
	$.vlpi.create = function()
	{
		$.vlpi.global.containingDiv = $('#appBody');

		//Load app settings from DB
		$.ajax({ 
	  		url:"/marc/ApplicationDataService",
			type:'POST',
			dataType:"json",
			data:{'requestType':'gatherApplicationSettings'},
			success: function(html)
			{
				var settings = html['AppSettings'];
				$('title').append(settings['appname'] + ' - ' + settings['parishname']);
				$('#marc_appNameH1').append(settings['appname'] + ' - ' + settings['parishname']);
				$('#marc_bottomnav').append(settings['appname'] + ' v' + settings['version'] + ' - Copyright ' + settings['copyrightYear']);
				
				//Detect cookie
				var loggedIn = false, user=null;
				var ca = document.cookie.split(';');
				for(var i=0; i<ca.length; i++)
				{
					var c=ca[i].trim();
					if(c.indexOf("user")==0)
					{
						user = c.substring(5,c.length);
						loggedIn = true;
					}
				}
				
				//Load view based on cookie availability
				if(loggedIn)
				{
					//Append the primary div for the modules
					$('#marc_nav').append($('<div>').attr('id','marc_navAccordion'));
							
					//parse through html[availableModules] after adding to ApplicationDataService results
					$.each(html['AvailableModules'], function(index, currMod)
					{
						//Import the required script
						$('head').append($('<script>').attr({'src':currMod['URL']}));
						
						//Append the module to the nav bar
						$('#marc_navAccordion')
							.append($('<h3>').text(currMod['Name']))
							.append($('<div>')
								.append($('<p>').append(currMod['Body']))
						);
					});
				}
				else
				{
					$('#marc_nav').append(
						$('<div>').attr('id','marc_navAccordion')
							.append($('<h3>').text("Log In"))
							.append($('<div>')
								.append($('<p>')
									.attr('id','loginFormParagraph')
									.append($('<form>')
										.attr({
											'action':'LoginServlet',
											'method':'post'
										})
										.append("Username: ")
											.append($('<input>').attr({'type':'text','name':'user'}))
										.append($('<br>'))
										.append("Password: ")
											.append($('<input>').attr({'type':'password','name':'pwd'}))
										.append($('<input>').attr({'type':'submit','value':'Login'}))
									)
								)
							)
					);
				}
				
				$('#marc_navAccordion').accordion();
			},
			error: function(html){
				alert("An error has occurred!");
			}
		}); //Closes ajax
	};
	
})(jQuery);